package com.lfs.adapter.base.ks;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuaishou.merchant.open.api.KsMerchantApiException;
import com.kuaishou.merchant.open.api.client.oauth.OauthAccessTokenKsClient;
import com.kuaishou.merchant.open.api.response.oauth.KsAccessTokenResponse;
import com.lfs.adapter.dto.PddProductPropertyDto;
import com.lfs.adapter.init.PopClientInit;
import com.lfs.adapter.service.PlatformManagerService;
import com.lfs.common.enums.EnumShopType;
import com.lfs.interfaces.dao.PlatformDao;
import com.lfs.interfaces.dto.LogisticsTemplateDto;
import com.lfs.interfaces.dto.TokenCreateDto;
import com.lfs.interfaces.model.PlatformEntity;
import com.lfs.interfaces.model.vo.LogisticsTemplateVo;
import com.lfs.interfaces.model.vo.MallInfoVo;
import com.lfs.interfaces.model.vo.TokenCreateVo;
import com.pdd.pop.sdk.http.PopAccessTokenClient;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsAuthorizationCatsRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsAuthorizationCatsResponse;
import com.pdd.pop.sdk.http.token.AccessTokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class KsPlatformManagerServiceImpl implements PlatformManagerService {

    @Autowired
    private PlatformDao platformDao;

    public KsPlatformManagerServiceImpl() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String releaseProduct() {
        return null;
    }

    @Override
    public List<LogisticsTemplateVo> getLogisticsTemplate(LogisticsTemplateDto logisticsTemplateDto) {
        return null;
    }

    @Override
    public MallInfoVo getMallInfo(TokenCreateDto tokenCreateDto) {
        return null;
    }

    @Override
    public String getGoodsCats() {
        // 获取当前授权商家可发布的商品类目信息
        return null;
    }

    @Override
    public String getGoodsRule() {
        return "";
    }

    @Override
    public String getCurrentCommodityCategory(String accessToken) {
        return null;
    }


    @Override
    public String searchPddBrand(String value, Long catId, Long refPid, String accessToken) {
        return null;
    }

    @Override
    public String getFreightTemplateData(String accessToken) {
        return null;
    }

    @Override
    public String getVideoData(String accessToken) {
        return null;
    }

    @Override
    public String getCurrentGoodsInfo(Long catId, String accessToken) {
        return null;
    }

    @Override
    public String getTwoPiecesDiscountRule(Long catId, String accessToken) {
        return null;
    }

    @Override
    public String getGoodsSpecifications(Long catId, String accessToken) {
        return null;
    }

    @Override
    public String getCustomSpecifications(Long parentSpecId, String specName, String accessToken) {
        return null;
    }

    @Override
    public String getGoodsCountry(String accessToken) {
        return null;
    }

    @Override
    public String goodsImgUpload(String fileUrl, String accessToken) {
        return null;
    }

    @Override
    public String filesPaceImgUpload(String fileUrl, String accessToken) {
        return null;
    }

    @Override
    public Map<String, Object> releaseGoods(PddProductPropertyDto pddProductPropertyDto, String accessToken) {
        return null;
    }

    @Override
    public String getPageCode(JSONObject jsonObject) {
        return "";
    }

    @Override
    public String getAuthUrl(TokenCreateDto tokenCreateDto) {
        PlatformEntity platform = platformDao.getPlatformById(tokenCreateDto.getPlatformId());
        String appKey = platform.getAppKey();
        return platform.getAuthUrl() + URLEncoder.encode(platform.getServerUrl() + appKey + "&redirect_url=" + platform.getRedirectUrl());
    }

    @Override
    public TokenCreateVo getAccessToken(TokenCreateDto tokenCreateDto) {
        TokenCreateVo tokenCreateVo = new TokenCreateVo();
        PlatformEntity platform = platformDao.getPlatformById(EnumShopType.PDD.getIndex());
        String appKey = platform.getAppKey();
        String appSecret = platform.getAppSecret();
        //不指定服务器地址，服务器地址默认为线上
        OauthAccessTokenKsClient oauthAccessTokenKsClient
                = new OauthAccessTokenKsClient(appKey, appSecret);
        // 生成AccessToken
        try {
            KsAccessTokenResponse response
                    = oauthAccessTokenKsClient.getAccessToken(tokenCreateDto.getCode());
            System.out.println(JSON.toJSONString(response));
            if (response.getError() != null) {
                log.info("/***************获取拼多多授权token错误：" + response.getErrorMsg() + "**************/");
            } else {
//                tokenCreateVo.setAccessToken(response.getAccessToken());
//                tokenCreateVo.setExpiresIn(response.getExpiresIn());
//                tokenCreateVo.setRefreshToken(response.getRefreshToken());
//                tokenCreateVo.setScope(response.getScope());
//                tokenCreateVo.setOwnerId(response.getOwnerId());
//                tokenCreateVo.setOwnerName(response.getOwnerName());
            }
        } catch (KsMerchantApiException e) {
            e.printStackTrace();
        }

        String refreshToken = "your app refreshToken";

        // 刷新AccessToken
        try {
            KsAccessTokenResponse response
                    = oauthAccessTokenKsClient.refreshAccessToken(refreshToken);
            System.out.println(JSON.toJSONString(response));
        } catch (KsMerchantApiException e) {
            e.printStackTrace();
        }
        return tokenCreateVo;
    }

}
